import sys
import os
import psutil
import time
import subprocess
import platform

def kill_program(pid):
    if platform.system() == 'Windows':
        cmd = 'taskkill -pid {} -f'.format(pid)
        os.system(cmd)
    else:#linux
        cmd = 'kill -9 {}'.format(pid)
        os.system(cmd)
        pass

def start_program(start_cmd:list):
    subprocess.Popen(start_cmd)

def update_repository():
    p = subprocess.Popen(["git","pull","origin","master"])
    p.wait()
    
if __name__ == '__main__':
    print("monitor:",sys.executable,sys.argv)
    print("executable:{}".format(sys.executable))
    for i,v in enumerate(sys.argv):
        print("sys.argv[{}]={}".format(i,v))
    order_type = sys.argv[1]
    pid = sys.argv[2]
    start_cmd = [sys.executable]
    start_cmd.extend(sys.argv[3:])
    if order_type == '机器人关机':
        kill_program(pid)
    elif order_type == '机器人重启':
        kill_program(pid)
        start_program(start_cmd)
    elif order_type == '机器人更新':
        kill_program(pid)
        update_repository()
        start_program(start_cmd)

    
    
    